package br.domsd.pocketcontrol.utils;

import java.io.File;
import java.util.ArrayList;

import br.domsd.pocketcontrol.model.Period;

public class Utils 
{

	public static ArrayList<String> checkMonthExpenses(File rootDir)
	{
		File fileDir = new File(rootDir, Constants.DIR_NAME);
		
		ArrayList<String> monthExpenses = new ArrayList<String>();
		
		String[] children = fileDir.list();
		
		if(children == null)
		{
			monthExpenses.add("Nothing to show");
		}
		else
		{
			for(int i = 0; i < children.length; i ++)
			{
				String formattedMonthAndYear = getMonthAndYearFormattedFromFileName(children[i]);
				monthExpenses.add(formattedMonthAndYear);
			}
		}
		
		return monthExpenses;
	}

	public static String getMonthFromFileName(String fileName)
	{
		String month = fileName.split("-")[0];
		
		return month;
	}
	
	public static String getYearFromFileName(String fileName)
	{
		String year = fileName.split("-")[1];
		year = year.replace(".xml", "");
		
		return year;
	}

	public static String getMonthFromPeriod(String period)
	{
		String month = period.split("/")[0];
		
		return month;
	}
	
	public static String getYearFromPeriod(String period)
	{
		String year = period.split("/")[1];

		return year;
	}
	
	public static String getMonthAndYearFormattedFromFileName(String fileName)
	{
		return getMonthFromFileName(fileName) + "/" + getYearFromFileName(fileName);
	}

	public static Period stringToPeriod(String periodString)
	{
		String month = getMonthFromPeriod(periodString);
		String year = getYearFromPeriod(periodString);
		
		Period period = new Period(month, year);
		
		return period;
	}
}
